package com.liu.eduService.controller;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.R;
import com.liu.eduService.entity.EduCourse;
import com.liu.eduService.entity.FrontVo.FrontCourseAllInfoVo;
import com.liu.eduService.entity.vo.CourseInfoVo;
import com.liu.eduService.entity.vo.CoursePublishVo;
import com.liu.eduService.entity.vo.CourseQuery;
import com.liu.eduService.service.EduCourseDescriptionService;
import com.liu.eduService.service.EduCourseService;
import com.liu.entity.CourseAllInfoVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author LSJ
 * @since 2022-01-07
 */
@RestController
@RequestMapping("/eduService/course")
@CrossOrigin
/**
 *  课程基本信息及 课程描述的 CRUD
 */
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;


    @PostMapping("addCourse")
    public R saveCourse(
            @ApiParam(name = "CourseInfoVo", value = "课程基本信息和课程描述", required = true)
            @RequestBody CourseInfoVo course){
        String courseID =  courseService.saveCourse( course);

        return R.ok().data("courseID",courseID);
    }

//    由课程ID查询课程信息
    @GetMapping("getCourse/{courseId}")
    public R getCourse(
            @ApiParam(name = "courseId", value = "课程id", required = true)
            @PathVariable String courseId){
        CourseInfoVo courseInfoVo = courseService.getCourse(courseId);
        return R.ok().data("CourseInfoVo",courseInfoVo);
    }

//被远程调用
    @GetMapping("getCourseAllInfo/{courseId}")
    public CourseAllInfoVo getCourseAllInfo(
            @ApiParam(name = "courseId", value = "课程id", required = true)
            @PathVariable String courseId){
//        CourseInfoVo course = courseService.getCourse(courseId);
//        System.out.println(course);
        FrontCourseAllInfoVo courseAllInfo = courseService.getCourseAllInfo(courseId);
//        System.out.println("=========="+courseAllInfo);
        CourseAllInfoVo courseAllInfoVo = new CourseAllInfoVo();
        BeanUtils.copyProperties(courseAllInfo,courseAllInfoVo);
//        System.out.println("++++++++++"+courseAllInfoVo);
        return courseAllInfoVo;
    }

//    修改课程信息
    @PostMapping("updateCourse")
    public R updateCourse(
            @RequestBody CourseInfoVo courseInfoVo
            ){
        Boolean update = courseService.updateCourse(courseInfoVo);
        return update?R.ok():R.error();
    }

    //    由课程ID查询课程信息
    @ApiOperation(value = "根据ID获取课程发布信息")
    @GetMapping("getCoursePublishInfo/{courseId}")
    public R getCoursePublishInfo(
            @ApiParam(name = "courseId", value = "课程id", required = true)
            @PathVariable String courseId){
        CoursePublishVo coursePublishInfo = courseService.getCoursePublishInfo(courseId);
        return R.ok().data("coursePublishInfo",coursePublishInfo);
    }

    //    修改课程信息
    @PostMapping("publishCourse/{courseId}")
    public R updateCoursePublishInfo(
            @PathVariable String courseId
    ){
        int update = courseService.publishCourse(courseId);
        return update>=0?R.ok():R.error();
    }


    @ApiOperation(value = "分页多条件查询课程")
    @PostMapping("pageCourseCondition/{page}/{limit}")
    public R PageConditionListForCourse(
            @ApiParam(name = "page",value = "页数",required = true)
            @PathVariable  Long page,
            @ApiParam(name = "limit",value = "显示条数",required = true)
            @PathVariable   Long limit,
            @ApiParam(name = "courseQuery",value = "课程条件",required = false)
            @RequestBody(required = false)
                    CourseQuery courseQuery){
        // 获取分页条件：
        Page<EduCourse> pageParam = new Page<>(page,limit);


        // 对 courseQuery 查询条件进行筛查
        courseService.pageQuery(pageParam,courseQuery);
        long total = pageParam.getTotal();

        List<EduCourse> records = pageParam.getRecords();

        return R.ok().data("total",total).data("records",records);
    }

    @DeleteMapping("{courseId}")
    public R DeleteCourseById(@PathVariable String courseId){
        Boolean delete = courseService.DeleteCourseById(courseId);
        return delete?R.ok():R.error();
    }
}

